package com.zookeeper.te3;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * 作者: LCG
 * 日期: 2020/2/26 23:29
 * 描述:
 */
public class ZkLock {

    public static void main(String[] args) throws Exception {

        RetryPolicy retryPolicy=new ExponentialBackoffRetry(3000,3);

        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);

        curatorFramework.start();

        InterProcessMutex interProcessMutex=new InterProcessMutex(curatorFramework,"/LOCK");

        interProcessMutex.acquire();//获取锁获取到的话运行否则阻塞

        System.out.println("=========================");

        interProcessMutex.release();//释放锁

    }

}
